package javacommon.util;

import java.util.Hashtable;
import java.util.Map;

/**
 * 处理菜单JSON串
 * 
 * @author pch 2010.5.26
 * 
 */
public class JsonUtil
{

   /**
    * 处理JSON串的辅助MAP
    */
   private static Map<String, Object> map;
   /**
    * 特征串开始符号
    */
   private static String ID_BEGIN = "{'id'";
   /**
    * 特征串结束符号
    */
   private static String ID_END = "}";
   /**
    * 比较的符号
    */
   public static String FILTER_URL = "trl";

   private static boolean lastFlag = false;

   /**
    * 组装要删除的串
    * 
    * @param allString
    *           总的菜单字符串
    * @param begin
    *           要删除的URL在字符串中的开始index
    * @param end
    *           要删除的URL在字符串中的结束index
    * @return map (其中menu:要删除的URL串)
    */
   private static Map encURL(String allString, int begin, int end)
   {
      if(map == null)
      {
         map = new Hashtable<String, Object>();
      }
      for(int j = (begin - 1); j >= 0; j--)
      {
         // 截取要删除URL之前的字符串
         String bebinStr = allString.substring(0, begin + 1);
         // 查找该URL对应的ID
         int idBegin = bebinStr.indexOf(ID_BEGIN, j);
         int idEnd = 0;
         if(idBegin >= 0)
         {
            // if(!lastFlag){
            // idEnd = allString.indexOf(ID_END, begin);
            // }else{
            // idEnd = allString.indexOf(ID_END, end + 1);
            // }

            idEnd = allString.indexOf(ID_END, end + 1);
            if(idEnd >= 0)
            {
               String menu = allString.substring(idBegin, idEnd + 1);
               map.put("menu", menu);
               map.put("begin", idBegin);
               map.put("end", idEnd);
               return map;
            }
         }
      }
      return map;
   }

   /**
    * 对需删除串的前后进行再次处理（二次组装）
    * 
    * @param map
    *           (menus:进行过滤的总串,menu:需删除的串,begin:menu所在的开始索引,end:menu所在的结束索引)
    * @return String 返回要删除的串
    */
   private static String findParent(Map<String, Object> map)
   {

      String str = (String) map.get("menus");
      String menu = (String) map.get("menu");
      int begin = (Integer) map.get("begin");
      int end = (Integer) map.get("end");
      // 串的尾部处理
      char sufix = 'c';
      String suf = null;

      if(begin >= 0)
      {
         sufix = str.charAt(end + 1);
         suf = String.valueOf(sufix);
         if(suf.equals(","))
         {
            menu = menu + ",";
         }
         else if(suf.equals("]"))
         {
            menu = "," + menu;
         }
      }

      // 串的二次处理
      if(end >= 0)
      {
         char prefix = str.charAt(begin - 1);
         String pre = String.valueOf(prefix);
         if(pre.equals("["))
         {
            if(suf.equals("]"))
            {
               if((begin - 2) >= 0)
               {
                  // lastFlag = true;
                  map = encURL(str, begin, end);
                  // lastFlag = false;
                  menu = findParent(map);
               }
            }
         }
      }

      return menu;
   }

   /**
    * 处理完成后的串
    * 
    * @param value
    *           需处理的串
    * @param equalString
    *           (url) 用来比较的串（利用该串组装要删除的串） [{'id':'1','name':'系统管理','
    *           nodes':[{'id':'101','name':'角色管理','trl':'pages/system/Role/index.jsp'}
    *           ,
    *           {'id':'102','name':'权限管理','trl':'pages/system/Permission/index.jsp'}
    *           , {
    *           'id':'103','name':'角色权限分配','trl':'pages/system/RolePermission/index.jsp'}
    *           ,
    *           {'id':'104','name':'操作员管理','trl':'pages/system/User/index.jsp'},
    *           {
    *           'id':'106','name':'系统通告','trl':'pages/sys/SysNotices/index.jsp'}
    *           , {
    *           'id':'108','name':'平台日志','trl':'pages/system/SysOperateLog/index.jsp'}],'open':false}
    *           , {'id':'2','name':'机构管理',' nodes
    *           ':[{'id':'201','name':'SI开通','trl':'pages/system/DataOrganizations/addSI.jsp'}
    *           , {
    *           'id':'202','name':'EC开通','trl':'pages/system/DataOrganizations/addEC.jsp'}
    *           , {
    *           'id':'203','name':'组织机构管理','trl':'pages/system/DataOrganizations/index.jsp'}
    *           , {
    *           'id':'204','name':'用户组织机构分配','trl':'pages/system/DataAccessPermission/index.jsp'}],'open':false}
    *           , {'id':'3','name':'信息统计',' nodes
    *           ':[{'id':'301','name':'医民互动流量统计','trl':'pages/stats/StatisticsSend/ymhdlltj.jsp'}
    *           , {
    *           'id':'302','name':'便民通知流量统计','trl':'pages/stats/StatisticsSend/index.jsp'},{'id':'303','name':'医民互动报送统计','trl':'pages/stats/StatisticsReport/index.jsp'}
    *           , {
    *           'id':'304','name':'便民通知报送统计','trl':'pages/stats/StatisticsReport/index.jsp'}
    *           , {'id':'307','name':'订购统计','trl':'pages/'}, {
    *           'id':'310','name':'订购查询','trl':'pages/socket/MiscCustomerOrder/index.jsp'}
    *           , {
    *           'id':'311','name':'退订查询','trl':'pages/socket/MiscCustomerOrderHis/index.jsp'}
    *           ,
    *           {'id':'312','name':'上行查询','trl':'pages/socket/CmppDeliver/index.jsp'}
    *           , {
    *           'id':'313','name':'下行查询','trl':'pages/socket/CmppSubmitIf/index.jsp'}],'open':false}
    *           , {'id':'4','name':'个人管理',' nodes
    *           ':[{'id':'401','name':'个人信息管理','trl':'pages/system/User/updateUser.jsp'}
    *           , {
    *           'id':'402','name':'机构信息管理','trl':'pages/system/DataOrganizations/updateEC.jsp'}
    *           ,
    *           {'id':'403','name':'修改密码','trl':'pages/system/User/updatePassword.jsp'}
    *           , {
    *           'id':'404','name':'密码重置','trl':'pages/system/User/resetPassword.jsp'}],'open':false}
    *           , {'id':'5','name':'基础数据','
    *           nodes':[{'id':'501','name':'地区信息表','trl':'pages/sys/SysArea/index.jsp'}
    *           ,
    *           {'id':'502','name':'号码段表','trl':'pages/sys/SysSection/index.jsp'}
    *           ,
    *           {'id':'503','name':'运营商','trl':'pages/sys/SysOperators/index.jsp'}
    *           ,
    *           {'id':'505','name':'客户资料分类','trl':'pages/sys/SysCustomer/index.jsp'}
    *           ,
    *           {'id':'506','name':'报送信息分类','trl':'pages/sys/SysMsgType/index.jsp'}
    *           ,
    *           {'id':'507','name':'部署参数','trl':'pages/sys/SysParam/index.jsp'},
    *           {'id':'508','name':'系统名称','trl':'pages/sys/SysName/index.jsp'},
    *           {
    *           'id':'509','name':'关键字管理','trl':'pages/sys/SysKeywords/index.jsp'}
    *           , {
    *           'id':'510','name':'默认群组管理','trl':'pages/sys/SysGroup/index.jsp'}],'open':false}
    *           , {'id':'6','name':'客户管理','
    *           nodes':[{'id':'601','name':'客户资料管理','trl':'pages/ae/Customer/index.jsp'}
    *           ,
    *           {'id':'602','name':'群组管理','trl':'pages/ae/EcOrganization/index.jsp'}
    *           , {
    *           'id':'603','name':'机构群组客户管理','trl':'pages/ae/EcGroupUser/index.jsp'}],'open':false}
    *           , {'id':'7','name':'信息中心',' nodes
    *           ':[{'id':'701','name':'信息报送','trl':'pages/message/EcReport/index.jsp'}
    *           ,
    *           {'id':'702','name':'便民通知','trl':'pages/message/EcReport/index2.jsp'}
    *           , {'id':'703','name':'信息审核',' nodes
    *           ':[{'id':'7031','name':'医民互动信息审核','trl':'pages/message/EcReportAudit/index.jsp'}
    *           , {
    *           'id':'7032','name':'便民通知审核','trl':'pages/message/EcReportAudit/index2.jsp'}
    *           , {
    *           'id':'7033','name':'留言回复审核','trl':'pages/message/VisitorsBook/audit.jsp'}],'open':false}
    *           , {'id':'704','name':'信息查询',' nodes
    *           ':[{'id':'7041','name':'医民互动发送查询','trl':'pages/socket/CmppSubmitIf/select.jsp'}],'open':false}
    *           , {'id':'705','name':'短信留言',' nodes
    *           ':[{'id':'7051','name':'留言管理','trl':'pages/message/VisitorsBook/index.jsp'}
    *           ,
    *           {'id':'7052','name':'留言审核','trl':'pages/message/VisitorsBook/audit.jsp'}
    *           , {
    *           'id':'7053','name':'留言回复','trl':'pages/message/VisitorsBook/reply.jsp'}],'open':false}
    *           , {'id':'706','name':'信息发送',' nodes
    *           ':[{'id':'7061','name':'医民互动发送','trl':'pages/message/EcReportSend/index.jsp'}
    *           , {
    *           'id':'7062','name':'便民通知发送','trl':'pages/message/EcReportSend/index2.jsp'}],'open':false}],'open':false}
    *           , {'id':'8','name':'样式例子',' nodes
    *           ':[{'id':'801','name':'测试生成模板页面','trl':'pages/demo/Demo/index.jsp'}],'open':false}
    *           ]
    * @return String 处理完成后的串
    */
   public static String endValid(String value, String equalString)
   {
      if(value.length() >= 0)
      {
         if(equalString.length() >= 0)
         {
            for(int i = 0; i < value.length(); i++)
            {
               int urlBegin = value.indexOf(equalString, 0);
               if(urlBegin >= 0)
               {
                  int urlEnd = value.indexOf("'", (urlBegin + equalString.length()));
                  if(urlEnd >= 0)
                  {
                     Map map = encURL(value, urlBegin, urlEnd + 1);
                     map.put("menus", value);
                     String delete = findParent(map);
                     // if(delete.substring(0, 1).equals(",")){
                     // int satar =value.indexOf(delete);
                     // String str =value.substring(satar+delete.length(),
                     // satar+delete.length()+15);
                     // if("],'open':false}".equals(str)&&"se}".equals(value.substring(satar-3,
                     // satar))){
                     // delete =delete+str;
                     // }
                     // }
                     value = value.replace(delete, "");
                     return value;
                  }
               }
            }
         }
      }
      return null;
   }

   public static String toMenus(String menus, String menuid)
   {
      String start = "{'id':'" + menuid + "'";
      int intStart = menus.indexOf(start);
      String end = "{'id':'" + (Integer.parseInt(menuid) + 1) + "'";
      int intEnd = 0;
      intEnd = menus.indexOf(end);
      if(intEnd <= 0)
      {
         intEnd = menus.length();
      }
      menus = menus.substring(intStart, intEnd - 1);
      menus = menus.replace("|", "");
      menus = "[" + menus + "]";
      return menus;
   }

}
